package com.zqx.mongodbdemo.controller;

import com.zqx.mongodbdemo.entity.User;
import com.zqx.mongodbdemo.repository.UserRepository;
import com.zqx.mongodbdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * description：usercontroller
 * @author zqx
 * @date 2018.08.02
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;


    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * mongodb 保存数据
     * @return
     */
    @GetMapping("/save")
    public User save()
    {
        User user = new User().setAge(12)
                .setId(1)
                .setName("Lisi");

        mongoTemplate.save(user);
        return user;
    }

    /**
     * mongodb查询列表
     * @return
     */
    @GetMapping("/find")
    public List<User> find()
    {
       return mongoTemplate.findAll(User.class);
    }

    /**
     * mongodb通过name查询数据
     * @param name
     * @return
     */
    @GetMapping("/findByName")
    public User findByName(@RequestParam("name")String name)
    {
        return userService.findByName(name);
    }
}
